%
% Mapping and localization
% - Mapping states
%

\section{Mapping and localization}

To decide what to do and which direction the robot is moving the map node listens to the state changes and the encoders. It decides how to interpret the encoders for the forward and backward movement, when to change the direction of the robot and when to adjust map position to a wall based on the states.

\subsection{Basic approach}
The basic structure of the map was from the beginning a simple matrix of booleans which were supposed to symbolize either free space or occupied/unknown space and the theory was to let the robot carve a path in the direction that it was heading. The default would be unknown space. For localization this part only relied on the simplification and assumption that it would always move straight forward and make straight angle turns was put in from the start. This condition is reinforced by keeping to walls that are shaped in that manner. The distance moved along a wall was computed by measuring the change in encoder values. \emph{Keeping to a wall} in this case means following a wall.
\\

The reason for explaining the first crude version without walls is because the final version is very much built from it and on the same principles. The map structure was updated to include more information in the form of an unsigned char array. To be more specific: free space, walls and unknown space. To place the walls IR sensor readings are used. However since most part of the mapping is done in the wall follower states and the wall follower adjusts its distance to the wall the IR readings had to be incorporated for localization as well. Therefore encoder readings are used to adjust the distance moved \emph{along a wall} but IR readings are used to adjust the distance \emph{to the wall}. Except in one case when the turn is done, a wall is to the right, then the IR readings are used to place the start point for the wall. After that the position is adjusted by the IR distance to that wall. This should be reliable since the wall is always straight and does not change position.


\subsection{Free space and walls}
The free space is painted by drawing free space from the robot to every point calculated by the IR sensor readings. The right side walls in the map are placed as described above but the left side is handled a little differently. The left side IR sensors are only used for putting walls and free space until the walls. The walls however are a threasholded median of consecutive points. This means that points are collected and when the direction changes or there is no wall reading the collected points are checked one by one. If a point is within the median of the previously collected then it will belong to the same wall. Otherwise it is believed to belong to a wall at another distance. This could be done in the same way for the right side, at least when there is no continuous wall to follow, and would possibly be more general but this is not the case for the final version. The adjustment to a continuous wall should still be done with the belief that it is the same wall that is followed.

\subsection{Localization in a map}
When a previous wall is found in the map in any stage and if it is within some threashold of where the IR sensors detect a wall, the position of the robot in the map is adjusted relative to the IR wall readings. To get this more accurate the idea was to also take the direction from which the wall was discovered into account. This however did not make it to the final version. Also the left wall IR readings should be used in the states not concerned with wall following to adjust the position. At the moment only the right ones are used.
\\

